package com.homedo.odin.solution.exception;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: zhaoxudong
 * @date: 2018/3/8 13:28
 * @Description: 统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有不可知的异常
     */
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultData exception(Exception e) {
        return new ResultData(Meta.CODE_500, e.getMessage());
    }

    /**
     * 处理业务异常
     */
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @ExceptionHandler(BizException.class)
    @ResponseBody
    public ResultData bizException(BizException e) {
        return e.getResultData();
    }
}
